<?php

class ContactForm extends Form
{
	public $MailModule = 'System/Controls/Forms';
	public $MailTemplate = 'templates/contactemail.tpl';
	
	public function ContactForm()
	{
		parent::__construct();
		
		$this->Name = 'contact';
		$this->Title = 'Contact';
		$this->ButtonText = 'Send contact email';
		
		// add controls to section
		$this->Controls[] = a2o( 'FormControl', array( 'FieldName' => 'fullname', 'Title' => 'Your Full name', 'IsBound' => false ) );		
		$this->Controls[] = a2o( 'FormControl', array( 'FieldName' => 'email', 'Title' => 'Your Email Address', 'IsBound' => false, 'Validator' => new EmailAddressValidator() ) );
		$this->Controls[] = a2o( 'FormControl', array( 'FieldName' => 'subject', 'Title' => 'Subject', 'IsBound' => false ) );		
		$this->Controls[] = a2o( 'FormControl', array( 'FieldName' => 'message', 'Title' => 'Your Message', 'IsBound' => false, 'Validator' => new StringLengthValidator(1), 'Template' => '/templates/FormControlTextArea.tpl' ) );		
	}
	
	public function FormAction()
	{
		// form load success?
		if( $this->LoadPostData())
		{
			$this->SendContactEmail( );
		}
	}

	public function SendContactEmail()
	{
		$FullName = $_POST['fullname'];
		$Email = $_POST['email'];
		$Subject = $_POST['subject'];
		$Message = $_POST['message'];
										
		// send email 
		$from = $_POST['fullname'] . '<' . $_POST['email'] . '>';
		$to = 'Martien de Jong<martiendejong2008@gmail.com';					
		$mailTemplate = a2o('TemplateLoader', array( 'Module' => $this->MailModule, 'Template' => $this->MailTemplate,
					'FullName' => $FullName, 'Message' => $Message ) );
		Mail::Send($to, $Subject, $mailTemplate->GetText( ));
		//mail($to, $this->MailSubject, $mailTemplate->GetText( ));
		//$this->Messages[] = $mailTemplate->GetText();
			
		// success
		$this->HideControls = true;
		$this->Messages[] = 'Your email message to Martien has been sent.';
	}
}
